package com.fierce.cloud.security.entity;

import com.fierce.cloud.ota.model.User;
import lombok.Data;
import org.apache.catalina.Role;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author: Fierce
 * @Date: 2020/12/3 14:16
 */

@Data
public class JwtUser {
    private Long id;
    private String userName;
    private String password;
    private Collection<? extends GrantedAuthority> authorities;

    public JwtUser(User user){
        this.id = user.getId();
        this.userName = user.getUsername();
        this.password = user.getPassword();
        List list = new ArrayList<SimpleGrantedAuthority>();
        user.getRoles().stream().forEach(role->list.add(new SimpleGrantedAuthority(role.getName())));
        this.authorities = list;
    }
}
